package com.ss.android.account.b;

import android.content.Context;
import android.os.AsyncTask;
import android.os.SystemClock;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.util.NetworkUtils;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONObject;

public final class ContactsAppData extends AsyncTask {
    private Context a;
    private int b;
    private List c;

    public ContactsAppData(Context arg2, List arg3) {

        this.a = arg2.getApplicationContext();
        this.c = arg3;
    }

    private Boolean a() {
        String v3_1;
        int v1_3;
        JSONObject v0_4;
        BufferedWriter v1;
        BufferedWriter v2_1 = null;
        Iterator v9;
        Object v0_2;
        Iterator v2;
        Context v8;
        Boolean v0_1 = false;
        List v7;
        Benchmark v6;
        long v4;
        BufferedWriter v3 = null;
        try {
            v4 = System.currentTimeMillis();
            v6 = new Benchmark("upload contacts");
            v7 = com.bytedance.common.utility.a.b.a(this.a, this.c);
            if(Logger.debug()) {
                v6.a("get phone list");
            }

            if(v7.size() == 0) {
                this.b = 1052;
                ContactsAppData.a(v4);
                v0_1 = Boolean.valueOf(false);
                return v0_1;
            }

            v8 = this.a;
            android.support.design.a.q = new LinkedHashMap();
            v2 = v7.iterator();
            do {
                if(!v2.hasNext()) {
                   break;
                }

                v0_2 = v2.next();
                if(v0_2 == null) {
                    continue;
                }

                if(((com.bytedance.common.utility.a.b.a)v0_2).b != null){
                    v9 = ((com.bytedance.common.utility.a.b.a)v0_2).b.iterator();
                    while(true) {
                        if(!v9.hasNext()) {
                            break;
                        }
                        Object v1_6 = v9.next();
                        if(android.support.design.a.isTextEmpty(((String)v1_6))) {
                            continue;
                        }
                        android.support.design.a.q.put(android.support.design.a.a(((String)v1_6), "SHA-256"), ((com.bytedance.common.utility.a.b.a)v0_2).a);
                    }
                }
            } while(true);

            try {
                v2_1 = new BufferedWriter(new OutputStreamWriter(v8.openFileOutput("cached_mobile_hash", 0)));
                Iterator<Map.Entry> v8_1 = android.support.design.a.q.entrySet().iterator();
                while (v8_1.hasNext()) {
                    v0_2 = v8_1.next();
                    v2_1.write((String) ((Entry) v0_2).getKey());
                    v2_1.write(" ");
                    v2_1.write((String) ((Entry) v0_2).getValue());
                    v2_1.newLine();
                }
            }catch (Exception e){

            }finally {
                if(v2_1 != null){
                    try{v2_1.close();}catch (Exception e){

                    }
                }
            }

            if(Logger.debug()) {
                v6.a("update cached mobile hash");
            }

            if(Logger.debug()) {
                Iterator v1_1 = v7.iterator();
                while(v1_1.hasNext()) {
                    v0_2 = v1_1.next();
                    Logger.v("ContactsHelper", "id: " + ((com.bytedance.common.utility.a.b.a)v0_2).c
                            + ", name: " + ((com.bytedance.common.utility.a.b.a)v0_2).a
                            + ", numbers: " + Arrays.deepToString(((com.bytedance.common.utility.a.b.a)v0_2).b.toArray()));
                }

                v6.a("print update cached mobile hash");
            }

            JSONArray v1_2 = new JSONArray();
            v2 = v7.iterator();
            while(v2.hasNext()) {
                v1_2.put(((com.bytedance.common.utility.a.b.a)v2.next()).a());
            }

            v0_4 = new JSONObject();
            v0_4.put("contacts", v1_2);
            this.b = 18;
            v1_3 = -1;
            v3_1 = NetworkUtils.executePost(v1_3, com.ss.android.account.aa.a, v0_4.toString().getBytes("UTF-8"), NetworkUtils.CompressType.GZIP, "application/json; charset=utf-8");
            if(android.support.design.a.isTextEmpty(v3_1)) {
                this.b = 1051;
                return Boolean.valueOf(false);
            }

            if(Logger.debug()) {
                v6.a("send contacts to server");
            }

            v0_4 = new JSONObject(v3_1);
            String v1_4 = v0_4.getString("message");
            if(!"success".equals(v1_4)) {
                if(("error".equals(v1_4)) && ("session_expired".equals(v0_4.getJSONObject("data").optString("name")))) {
                    this.b = 105;
                    return Boolean.valueOf(false);
                }

                Logger.w("ContactsAppData", "upload contacts failed: " + v3_1 + " caused by error : " + String.valueOf(this.b));
                return Boolean.valueOf(false);
            }

            if(Logger.debug()) {
                v6.a("parse upload result");
                v6.a();
            }

            SystemClock.sleep(1000);
            ContactsAppData.a(v4);
            return Boolean.valueOf(true);
        } catch(Exception v0) {
            v0.printStackTrace();
        }
        return v0_1;
    }

    private static void a(long arg6) {
        long v4 = 1500;
        long v0 = System.currentTimeMillis() - arg6;
        if(v0 < v4) {
            SystemClock.sleep(v4 - v0);
        }
    }

    protected final Object doInBackground(Object[] arg2) {
        return this.a();
    }

    public final void onPostExecute(Object arg1) {
    }
}

